//
//  VCOfAnchor.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/3.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfAnchor.h"
#import "XYMacro.h"

@interface VCOfAnchor () <UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, TopPageBarDelegate>
@property (nonatomic, strong) UIScrollView *myScrollView;
@property (nonatomic, strong) UIView *contentViewOne;
@property (nonatomic, strong) BaseCollectionView *contentViewTwo;
@property (nonatomic, strong) UIView *customHeaderView;
@property (nonatomic, strong) DiscoverTopPageBar *typeBar;
@property (nonatomic, strong) AnchorInformationCustomView *sexView;
@property (nonatomic, strong) AnchorInformationCustomView *ageVeiw;
@property (nonatomic, strong) AnchorInformationCustomView *brithVeiw;
@property (nonatomic, strong) AnchorInformationCustomView *constellationView;
@property (nonatomic, strong) UIImageView *headerImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *informationLabel;
@property (nonatomic, assign) BOOL isSecond;
@property (nonatomic, strong) AnchorModelFirst *model;
@property (nonatomic, strong) AnchorModelSecond *modelLanmu;


@end

@implementation VCOfAnchor

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self configNavigationController];
    [self configViewController];
    self.isSecond = NO;
}

- (void)handleData {
    
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22user_id%22%3A%22";
    NSString *str2 = @"%22%7D";
    NSString *body = [[str1 stringByAppendingString:_item_managerid] stringByAppendingString:str2];
    
    [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/social/userinfo.jhtml" andBody:body andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
        _model = [AnchorModelFirst modelWithDictionary:result];
        
        self.title = [NSString stringWithFormat:@"%@的主页", _model.data.info.nickname];
        if ([_model.data.info.gender isEqualToString:@"1"]) {
            _sexView.contentLabel.text = @"男";
        } else {
            _sexView.contentLabel.text = @"女";
        }
        _ageVeiw.contentLabel.text = [NSString stringWithFormat:@"%ld", _model.data.info.age];
        _brithVeiw.contentLabel.text = _model.data.info.birthday;
        
        _constellationView.contentLabel.text = _model.data.info.constellation;

        _nameLabel.text = _model.data.info.nickname;
        
        [_headerImageView sd_setImageWithURL:[NSURL URLWithString:_model.data.info.userimg] placeholderImage:PLACEHOLDERIMAGE];
        
        
        NSString *str3 = @"json=%7B%22cat_id%22%3A%220%22%2C%22orderby%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%2C%22searchtype%22%3A2%2C%22session%22%3A%7B%22uid%22%3A%2233376%22%2C%22sid%22%3A%22f219a5f641104487a9cb7b437c190335%22%7D%2C%22keyword%22%3A%22%22%2C%22target_id%22%3A%22";
        NSString *str4 = @"%22%7D";
        NSString *body2 = [[str3 stringByAppendingString:_model.data.info.id] stringByAppendingString:str4];
        
        [XYNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:body2 andBodyStyle:XYBodyString andHeader:URLHEADER andResponse:XYJSON andSuccessBlock:^(id result) {
            
            _modelLanmu = [AnchorModelSecond modelWithDictionary:result];
            [_contentViewTwo reloadData];
            
        } andFailureBlock:^(NSError *error) {}];
        [_contentViewTwo reloadData];

        
    } andFailureBlock:^(NSError *error) {}];
}


- (void)configNavigationController {
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackButton.frame = CGRectMake(0, 0, 30, 30);
    [goBackButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [goBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:goBackButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    
    
}

- (void)configViewController {
    self.view.backgroundColor = BACKGROUNDCOLOR;
    
    [self createScrollView];
    [self createCustomHeaderView];
    [self createContentViewOne];
    [self creatContentViewTwo];
    
}


- (void)createScrollView {
    _myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _myScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 2, _myScrollView.frame.size.height);
    _myScrollView.pagingEnabled = YES;
    _myScrollView.bounces = NO;
    _myScrollView.delegate = self;
    _myScrollView.showsHorizontalScrollIndicator = NO;
    _myScrollView.backgroundColor = [UIColor yellowColor];
    _myScrollView.scrollEnabled = NO;
    
    [self.view addSubview:_myScrollView];
}



- (void)createContentViewOne {
    _contentViewOne = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];

    _contentViewOne.backgroundColor = [UIColor whiteColor];
    [_myScrollView addSubview:_contentViewOne];
    
    
    _sexView = [[AnchorInformationCustomView alloc] initWithFrame:CGRectMake(0, 300, SCREEN_WIDTH, 50) title:@"性别"];
    [_contentViewOne addSubview: _sexView];
    
    _ageVeiw = [[AnchorInformationCustomView alloc] initWithFrame:CGRectMake(0, 350, SCREEN_WIDTH, 50) title:@"年龄"];
    [_contentViewOne addSubview:_ageVeiw];
    
    _brithVeiw = [[AnchorInformationCustomView alloc] initWithFrame:CGRectMake(0, 400, SCREEN_WIDTH, 50) title:@"出生年月"];
        [_contentViewOne addSubview:_brithVeiw];

    _constellationView = [[AnchorInformationCustomView alloc] initWithFrame:CGRectMake(0, 450, SCREEN_WIDTH, 50) title:@"星座"];
    [_contentViewOne addSubview:_constellationView];
    
    
}


- (void)creatContentViewTwo {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 110) / 3, (SCREEN_WIDTH - 110) / 3);
    layout.minimumLineSpacing = 50;
    layout.minimumInteritemSpacing = 35;
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _contentViewTwo = [[BaseCollectionView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) collectionViewLayout:layout];
    _contentViewTwo.delegate = self;
    _contentViewTwo.dataSource = self;
    _contentViewTwo.contentInset = UIEdgeInsetsMake(260, 0, 0, 0);
    _contentViewTwo.backgroundColor = [UIColor whiteColor];
    [_myScrollView addSubview:_contentViewTwo];
    [_contentViewTwo registerClass:[AnchorCollectionViewCellFirst class] forCellWithReuseIdentifier:@"AnchorCollectionViewCellFirst"];
    
    
}

- (void)createCustomHeaderView {
    _customHeaderView = [[UIView alloc] init];
    _customHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 260);
    [self.view addSubview:_customHeaderView];
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"touxiang_bg"]];
    backImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 210);
    [_customHeaderView addSubview:backImageView];
    
    _typeBar = [[DiscoverTopPageBar alloc] initWithFrame:CGRectMake(0, 210, SCREEN_WIDTH, 50) withTitleArr:@[@"资料", @"栏目"]];
    _typeBar.delegate = self;
    [_customHeaderView addSubview:_typeBar];
    
    _headerImageView = [[UIImageView alloc] init];
    _headerImageView.frame = CGRectMake(170, 40, SCREEN_WIDTH - 340,  SCREEN_WIDTH - 340);
    _headerImageView.clipsToBounds = YES;
    _headerImageView.layer.cornerRadius = (SCREEN_WIDTH - 340) / 2;
    _headerImageView.layer.borderWidth = 2;
    _headerImageView.layer.borderColor = [UIColor redColor].CGColor;
    _headerImageView.userInteractionEnabled = YES;
    [_customHeaderView addSubview:_headerImageView];
    
    UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextPage:)];
    [_headerImageView addGestureRecognizer:imageTap];
    
    
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(150, SCREEN_WIDTH - 300 + 10, SCREEN_WIDTH - 300, 30)];
    _nameLabel.textColor = [UIColor whiteColor];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    [_customHeaderView addSubview:_nameLabel];
    
    _informationLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, SCREEN_WIDTH - 250, SCREEN_WIDTH - 100, 20)];
    _informationLabel.font = [UIFont systemFontOfSize:15];
    _informationLabel.textColor =[UIColor colorWithRed:0.65 green:0.62 blue:0.59 alpha:1.00];
    _informationLabel.textAlignment = NSTextAlignmentCenter;
    [_customHeaderView addSubview:_informationLabel];
    
    
}




- (void)createKVO {
    //KVO
    [_myScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    CGFloat newX = [[change objectForKey:@"new"] CGPointValue].x;
    CGFloat oldX = [[change objectForKey:@"old"] CGPointValue].x;
    
    CGFloat redX = (newX / SCREEN_WIDTH) * (SCREEN_WIDTH / 2);
    // 修改Redline位置.
    self.typeBar.redLineView.transform = CGAffineTransformMakeTranslation(redX, 0);
    
    DiscoverCollectionViewCellForTopPageBar *cell1 = (DiscoverCollectionViewCellForTopPageBar *)[_typeBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:newX / SCREEN_WIDTH inSection:0]];
    DiscoverCollectionViewCellForTopPageBar *cell2 = (DiscoverCollectionViewCellForTopPageBar *)[_typeBar.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:oldX / SCREEN_WIDTH inSection:0]];
    cell2.titleLabel.textColor = [UIColor grayColor];
    cell1.titleLabel.textColor = [UIColor whiteColor];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == _contentViewTwo && self.isSecond == YES) {
        if (_contentViewTwo.contentOffset.y >= -50.f) {
            _typeBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
            [self.view addSubview:_typeBar];
        } else {
            _typeBar.frame = CGRectMake(0, 210, SCREEN_WIDTH, 50);
            [_customHeaderView addSubview:_typeBar];
            
        }
    }
    
}



- (void)didClickItemindexPath:(NSIndexPath *)indexPath PageBar:(DiscoverTopPageBar *)pageBar {
    self.myScrollView.contentOffset = CGPointMake(SCREEN_WIDTH * indexPath.row, 0);
    if (indexPath.item == 0) {
        _typeBar.frame = CGRectMake(0, 210, SCREEN_WIDTH, 50);
        [_customHeaderView addSubview:_typeBar];
        _customHeaderView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 260);
        [_contentViewOne addSubview:_customHeaderView];
        self.isSecond = NO;
    }
    if (indexPath.item == 1) {
        if (_isSecond == NO) {
            _contentViewTwo.contentOffset = CGPointMake(0, -260);
        }
        _customHeaderView.frame = CGRectMake(0, -260, SCREEN_WIDTH, 260);
        [_contentViewTwo addSubview:_customHeaderView];
        self.isSecond = YES;
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_modelLanmu.status.succeed == 1) {
        return _modelLanmu.data.count;
    } else {
        return 0;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    AnchorCollectionViewCellFirst *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AnchorCollectionViewCellFirst" forIndexPath:indexPath];
    AnchorModelSecondData *model = [_modelLanmu.data objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == _contentViewTwo) {
        
        VCOfDiscoverList *nextVC = [[VCOfDiscoverList alloc] init];
        AnchorModelSecondData *model = [_modelLanmu.data objectAtIndex:indexPath.row];
        nextVC.item_id = [NSString stringWithFormat:@"%ld", model.item_id];
        [self.navigationController pushViewController:nextVC animated:YES];
        
    }
    
}


- (void)goBack:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    NSLog(@"%@", [self.navigationController.viewControllers lastObject])
    if ([[self.navigationController.viewControllers lastObject] isKindOfClass:[VCOfDiscover class]]) {
        self.tabBarController.tabBar.hidden = NO;
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.94 green:0.00 blue:0.00 alpha:1.00];
        
    } else {
        self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    }
        
}

- (void)nextPage:(UITapGestureRecognizer *)tap {
    VCOfHeaderPicture *nextVC = [[VCOfHeaderPicture alloc] init];
    nextVC.picUrl = _model.data.info.userimg;
    nextVC.titleStr = _model.data.info.nickname;
    [self.navigationController pushViewController:nextVC animated:YES];
}


- (void)viewDidAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    [self createKVO];
}


- (void)viewWillDisappear:(BOOL)animated {
    [_myScrollView removeObserver:self forKeyPath:@"contentOffset"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
